<?php 
$req_path="../";
require_once($req_path."includes/db.php");

class M_Course extends DB{
    private $rowCount=0;
    public $table_name="tb_course";
	
    function M_Course(){
		parent::__construct();
    }

    function loadData($year='',$term='',$status=0,$title='',$id=0){
		$sqlscript= " select c.id,year, term , teacher, c.subject_id , s.name as subject_name , month_from, month_to
					, c.detail, cost, py.name as type, py.id as type_id, remark, c.status,c.remark2, c.course_type, c.teacher,c.teacher2
					from tb_course c
					join tb_subject s on s.id = c.subject_id 
					LEFT JOIN tb_type_payment_service py on py.id = c.`type`
					WHERE 1=1 ";
        
		if ($year!=''){
			$sqlscript .= " AND c.year=". $year ;
		}
		if ($term!=''){
			$sqlscript .= " AND c.term=". $term ;
		}
		if ($status!='0'){
			$sqlscript .= " AND c.status='". $status."'" ;
		}
		if ($title!=''){
			$sqlscript .= " AND c.detail like '%". $title."%'" ;
		}
		if ($id !=0){
			$sqlscript .= " AND c.id=".$id;
		}
		
		$sqlscript.=" order by c.createdate desc ";
        $result = $this->ExecuteData($sqlscript);
        $this->rowCount = $result->RecordCount();
        return $result;
    }
	function getDropdownSubject(){
		$sql=" select id , name
				from tb_subject
				where status = 'Y'" ;
				
		$result = $this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["name"];
		}
		return $ar;	
	}
	function getClass(){
		$ar=array();
		$sql=" SELECT id, name
				FROM tb_class_level
				where status = 'Y'" ;
		
		$result = $this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["id"]] = $row["name"];
		}
		return $ar;	
	}
	function getClasslist($id=''){
		$sql=" SELECT id, alian as name
				FROM tb_class_level
				where status = 'Y'" ;
		$sql.=" AND id in (".$id.")";
		$list_class="";
		if ($id != ''){
		   $result = $this->ExecuteData($sql);
			while($row = $result->FetchRow()){
				$list_class .= $row["name"]. ", "; 
			}
			$list_class = substr($list_class, 0, -2);
		}
		return $list_class;	
	}
	function getDate($list=''){
		$sql = " SELECT value FROM tb_date WHERE id IN (".$list.")";
		$list_date ="";
		if ($list !=''){
		    $result = $this->ExecuteData($sql);
			while($row = $result->FetchRow()){
				$list_date .= $row["value"]. " - ";
			}
			$list_date = substr($list_date, 0, -2);
		}
		
		return $list_date;	
	
	}
	function load_detail_data($id){
		$sql = "SELECT cd.id ,cd.for_class , cd.study_date, cd.study_time ,cd.study_time_end , DATE_FORMAT(cd.start_date,'%d/%m/%Y') startdate, c.remark,c.remark2
				, DATE_FORMAT(cd.end_date,'%d/%m/%Y') enddate
				, DATE_FORMAT(cd.start_date,'%m') start_month
				, DATE_FORMAT(cd.start_date,'%d') start_day
				, DATE_FORMAT(cd.start_date,'%y') start_year
				, DATE_FORMAT(cd.end_date,'%m') end_month
				, DATE_FORMAT(cd.end_date,'%d') end_day
				, DATE_FORMAT(cd.end_date,'%y') end_year
				, ifnull(month_from,0) as month_from , ifnull(month_to,0) as  month_to
				FROM tb_course  c
				JOIN tb_course_detail cd on c.id = cd.course_id";
		$sql .= " WHERE 1=1 AND c.id=".$id;
		
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	
	function getCourse_CBO(){
		$ar=array();
		$sql="SELECT   s.name as subject_name , c.id course_id, c.detail
			FROM tb_course  c
			JOIN tb_subject s on s.id = c.subject_id
			WHERE c.status='Y'
			group by s.name, c.id, c.detail" ;
		
		$result = $this->ExecuteData($sql);
		while($row = $result->FetchRow()){
			$ar[$row["course_id"]] = $row["subject_name"] .' '.$row['detail'];
		}
		return $ar;
	}
	function insertCourse($val){
		if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT course_type, year, term ,subject_id ,teacher,teacher2, month_from, month_to, cost,type, detail , remark,remark2,  status ,createdate,createby FROM tb_course where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
				$id = $this->getLastID();
				return $id;
            }
	}
	function insertCourseDetail($val){
		if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT course_id, for_class, study_date , study_time ,study_time_end , start_date ,end_date  FROM tb_course_detail where 1=2 ", $val);
				print_r($sqlInsert);
				$this->ExecuteData($sqlInsert);
            }
	}
	
	function getDropdownCarDetail($id){
		$sql =" SELECT detail ";
		$sql.=" FROM tb_car WHERE id=".$id;
		
		$result = $this->ExecuteData($sql);
		$row = $result->FetchRow();
		
		return $row;	
	}
    function getRowCount(){
       return $this->rowCount;
    }	
 
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT license, detail ,amount ,status ,createdate,createby FROM tb_car where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT course_type,year, term ,subject_id ,teacher,teacher2, cost,type, detail , month_from, month_to, remark,remark2, status , updatedate, updateby FROM tb_course where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				 
				$this->deleDetail($id);
            }
    }
	function deleDetail($id){
		$sql_detail = " DELETE FROM tb_course_detail where course_id=".$id;
		$this->ExecuteData($sql_detail);
	}
    function deleData($id){
		if(isset($id)){
			$sqlDelete = "Delete FROM tb_course where id='".$id."'";
			$this->ExecuteData($sqlDelete);
			
			$this->deleDetail($id);
			return 1;
		}	
    }
	function loadFee(){
		$sql= " SELECT fix1, fix2, fix3, fix4,fix5,fix6 from tb_course_fee limit 0,1 ";
		$this->ExecuteData($sql);
		
		$result = $this->ExecuteData($sql);
		$row = $result->FetchRow();
		
		return $row;	
	}
    function updateFixdata($ar){
			 
			$sqlupdate = $this->UpdateSqlScript("SELECT fix1,fix2,fix3,fix4,fix5,fix6 FROM tb_course_fee ",$ar);
			$this->ExecuteData($sqlupdate);
			
		return 1;
		 
	
	}
}
?>
